namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceEventsPageEdit : Page
    {
        protected DropDownList DayDdl;
        protected DropDownList EventsCityDdl;
        protected TextBox EventsNameTxb;
        protected DropDownList EventsProvinceDdl;
        protected TextBox EventsTopicTxb;
        protected Image iResource;
        protected Label lbDay;
        protected Label lbEventsCity;
        protected Label lbEventsName;
        protected Label lbEventsProvince;
        protected Label lbEventsTime;
        protected Label lbEventsTopic;
        protected Label lbMonth;
        protected Label lbYear;
        protected DropDownList MonthDdl;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;
        protected TextBox YearTxb;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["EventsId"]);
                ResourceEvents resourceEvents = ManageResource.GetResourceEventsInfoViaId(id);
                this.EventsNameTxb.Text = resourceEvents.EventsName;
                this.EventsTopicTxb.Text = resourceEvents.EventsTopic;
                if (resourceEvents.EventsTime.Year.ToString() != "1800")
                {
                    this.YearTxb.Text = resourceEvents.EventsTime.Year.ToString();
                }
                if ((resourceEvents.EventsTime.Month == 1) && (resourceEvents.EventsTime.Day == 1))
                {
                    this.MonthDdl.SelectedIndex = 0;
                    this.DayDdl.SelectedIndex = 0;
                }
                else
                {
                    for (int i = 0; i < this.MonthDdl.Items.Count; i++)
                    {
                        if (this.MonthDdl.Items[i].Value == resourceEvents.EventsTime.Month.ToString())
                        {
                            this.MonthDdl.SelectedIndex = i;
                        }
                    }
                    for (int i = 0; i < this.DayDdl.Items.Count; i++)
                    {
                        if (this.DayDdl.Items[i].Value == resourceEvents.EventsTime.Day.ToString())
                        {
                            this.DayDdl.SelectedIndex = i;
                        }
                    }
                }
                for (int i = 0; i < this.EventsProvinceDdl.Items.Count; i++)
                {
                    if (this.EventsProvinceDdl.Items[i].Value == resourceEvents.EventsProvince.ToString())
                    {
                        this.EventsProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.EventsCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceEvents.EventsProvince);
                this.EventsCityDdl.DataTextField = "regionName";
                this.EventsCityDdl.DataValueField = "regionId";
                this.EventsCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.EventsCityDdl.Items.Count; i++)
                {
                    if (this.EventsCityDdl.Items[i].Value == resourceEvents.EventsCity.ToString())
                    {
                        this.EventsCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.EventsCityDdl.Items.Clear();
                }
                for (int i = 0; i < this.EventsCityDdl.Items.Count; i++)
                {
                    if (this.EventsCityDdl.Items[i].Value == resourceEvents.EventsCity.ToString())
                    {
                        this.EventsCityDdl.SelectedIndex = i;
                    }
                }
            }
        }

        private void HolidayProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.EventsCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.EventsProvinceDdl.SelectedItem.Value));
            this.EventsCityDdl.DataTextField = "regionName";
            this.EventsCityDdl.DataValueField = "regionId";
            this.EventsCityDdl.DataBind();
        }

        private void InitializeComponent()
        {
            this.EventsProvinceDdl.SelectedIndexChanged += new EventHandler(this.HolidayProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.EventsProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.EventsProvinceDdl.DataTextField = "RegionName";
            this.EventsProvinceDdl.DataValueField = "RegionId";
            this.EventsProvinceDdl.DataBind();
            this.EventsCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.EventsProvinceDdl.SelectedItem.Value));
            this.EventsCityDdl.DataTextField = "regionName";
            this.EventsCityDdl.DataValueField = "regionId";
            this.EventsCityDdl.DataBind();
            this.MonthDdl.Items.Clear();
            this.DayDdl.Items.Clear();
            ListItem tmpItem1 = new ListItem();
            tmpItem1.Text = "--";
            this.MonthDdl.Items.Add(tmpItem1);
            this.DayDdl.Items.Add(tmpItem1);
            for (int i = 1; i <= 12; i++)
            {
                ListItem MonthItem = new ListItem();
                MonthItem.Text = i.ToString();
                this.MonthDdl.Items.Add(MonthItem);
            }
            for (int i = 1; i <= 0x1f; i++)
            {
                ListItem DateItem = new ListItem();
                DateItem.Text = i.ToString();
                this.DayDdl.Items.Add(DateItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            DateTime eventTime;
            string strEventsName = this.EventsNameTxb.Text.Trim();
            string strEventsTopic = this.EventsTopicTxb.Text.Trim();
            try
            {
                eventTime = new DateTime(Convert.ToInt32(this.YearTxb.Text.Trim()), Convert.ToInt32(this.MonthDdl.SelectedItem.Text), Convert.ToInt32(this.DayDdl.SelectedItem.Text));
            }
            catch (Exception)
            {
                eventTime = new DateTime(0x708, 1, 1);
            }
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strEventsName != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.EventsCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.EventsCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceEventsInfo(new Guid(base.Request.QueryString["EventsId"]), strEventsName, eventTime, new Guid(this.EventsProvinceDdl.SelectedItem.Value), cityId, strEventsTopic);
                    base.Response.Redirect("~/CIS/Resource/ResourceEventsPage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strEventsName != "")
            {
                if (!ManageResource.IsExistSameResourceEventsInfo(strEventsName))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.EventsCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.EventsCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceEventsInfo(strEventsName, eventTime, new Guid(this.EventsProvinceDdl.SelectedItem.Value), cityId, strEventsTopic);
                    base.Response.Redirect("~/CIS/Resource/ResourceEventsPage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbEventsName.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsName", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbEventsProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsProvinceOverSea", language, path);
                this.lbEventsCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsCityOverSea", language, path);
            }
            else
            {
                this.lbEventsProvince.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsProvince", language, path);
                this.lbEventsCity.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsCity", language, path);
            }
            this.lbEventsTime.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsTime", language, path);
            this.lbEventsTopic.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbEventsTopic", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "SaveBtn", language, path);
            this.lbMonth.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbMonth", language, path);
            this.lbDay.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbDay", language, path);
            this.lbYear.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceEventsPageEdit", "lbYear", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }
    }
}

